package com.yunmang.activity;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.yunmang.R;
import com.yunmang.adapter.SortListAdapter;
import com.yunmang.app.AppConstant;
import com.yunmang.entity.EndModel;
import com.yunmang.entity.LoginBean;
import com.yunmang.entity.event.EndEvent;
import com.yunmang.utils.PinyinComparator;
import com.yunmang.view.ClearEditText;
import com.yunmang.view.SideBar;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import de.greenrobot.event.EventBus;

/**
 * Created by Administrator on 2017/5/3.
 */
public class SearchEndActivity extends BaseActivity {
    @InjectView(R.id.filter_edit)
    ClearEditText filterEdit;
    @InjectView(R.id.country_lvcountry)
    ListView sortListView;
    @InjectView(R.id.dialog)
    TextView dialog;
    @InjectView(R.id.sidebar)
    SideBar sidebar;
    private List<EndModel> sourceData;
    private SortListAdapter adapter;
    private String start;
    /**
     * 根据拼音来排列ListView里面的数据类
     */
    private PinyinComparator pinyinComparator;
    @Override
    public int getLayoutId() {
        return R.layout.activity_searchend;
    }

    @Override
    public void initView() {
        ButterKnife.inject(this);
        pinyinComparator=new PinyinComparator();
    }

    @Override
    public void initData() {
        if(getIntent()!=null){
            start=getIntent().getStringExtra("start")==null?"":"start";
        }
        getAllEndStation();
        fillData(AppConstant.endOrgList);
        Collections.sort(sourceData,pinyinComparator);
        adapter=new SortListAdapter(this,sourceData);
        sortListView.setAdapter(adapter);
        sidebar.setTextView(dialog);
        //设置右侧触摸监听
        sidebar.setOnTouchingLetterChangedListener(new SideBar.OnTouchingLetterChangedListener() {

            @Override
            public void onTouchingLetterChanged(String s) {
                //该字母首次出现的位置
                int position = adapter.getPositionForSection(s.charAt(0));
                if(position != -1){
                    sortListView.setSelection(position);
                }
            }
        });

        sortListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
                EndModel model = (EndModel) adapter.getItem(position);
                if(start.equals("start")){
                    Intent in=new Intent();
                    in.putExtra("id",model.subModel.getId());
                    in.putExtra("name",model.subModel.getName());
                    setResult(110,in);
                }else{
                    EventBus.getDefault().post(new EndEvent(model.subModel.getName(),model.subModel.getId()));
                }

                finish();
            }
        });
    }

    /**
     * 构造数据
     */
    private void fillData(List<LoginBean.ModelBean.EndOrgListBean> endlist) {
        sourceData=new ArrayList<>();
        for (LoginBean.ModelBean.EndOrgListBean bean:endlist) {
            EndModel model=new EndModel();
            model.subModel=bean;
            String first=bean.getSpelling().substring(0,1).toUpperCase();
            if(first.matches("[A-Z]")){
                model.firstLetter=first;
            }else{
                model.firstLetter="#";
            }
            sourceData.add(model);
        }
    }

    /**
     * 从本地拉取所有的到站
     */
    private void getAllEndStation() {
    }

}
